package top.mybatisx.mpquery.toolkit;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * sha1 签名算法
 */
public class Sha1Util {

    public static String sha1(String input) {
        byte[] inputBytes = input.getBytes();

        // 创建SHA-1消息摘要对象
        MessageDigest sha1Digest = null;
        try {
            sha1Digest = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        // 计算消息摘要
        byte[] sha1Bytes = sha1Digest.digest(inputBytes);

        // 将字节数组转换为十六进制字符串
        StringBuilder sb = new StringBuilder();
        for (byte b : sha1Bytes) {
            sb.append(String.format("%02x", b));
        }
        // 输出SHA-1摘要结果: 必须是字符串开头
        return "s" + sb.toString();
    }
}
